<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 订单
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>

        <div class="container">
            <div class="handle-box">
                <el-button type="primary" style="float: left; " icon="el-icon-delete" class="handle-del mr10"  @click="delAllSelection" >批量删除</el-button>

                <el-col :span="9" style="float: right; ">
                    <el-select v-model="query.address" placeholder="地址" class="handle-select mr10">
                        <el-option key="1" label="安徽省" value="安徽省"></el-option>
                        <el-option key="2" label="北京" value="北京"></el-option>
                        <el-option key="3" label="重庆" value="重庆"></el-option>
                        <el-option key="4" label="福建省" value="福建省"></el-option>
                        <el-option key="5" label="广东省" value="广东省"></el-option>
                        <el-option key="6" label="甘肃省" value="甘肃省"></el-option>
                        <el-option key="7" label="贵州省" value="贵州省"></el-option>
                        <el-option key="8" label="海南省" value="海南省"></el-option>
                        <el-option key="9" label="河北省" value="河北省"></el-option>
                        <el-option key="10" label="河南省" value="河南省"></el-option>
                        <el-option key="11" label="黑龙江省" value="黑龙江省"></el-option>
                        <el-option key="12" label="湖北省" value="湖北省"></el-option>
                        <el-option key="13" label="湖南省" value="湖南省"></el-option>
                        <el-option key="14" label="吉林省" value="吉林省"></el-option>
                        <el-option key="15" label="江苏省" value="江苏省"></el-option>
                        <el-option key="16" label="江西省" value="江西省"></el-option>
                        <el-option key="17" label="辽宁省" value="辽宁省"></el-option>
                        <el-option key="18" label="内蒙古" value="内蒙古"></el-option>
                        <el-option key="19" label="青海省" value="青海省"></el-option>
                        <el-option key="20" label="山东省" value="山东省"></el-option>
                        <el-option key="21" label="山西省" value="山西省"></el-option>
                        <el-option key="22" label="陕西省" value="陕西省"></el-option>
                        <el-option key="23" label="上海" value="上海"></el-option>
                        <el-option key="24" label="四川省" value="四川省"></el-option>
                        <el-option key="25" label="天津" value="天津"></el-option>
                        <el-option key="26" label="西藏" value="西藏"></el-option>
                        <el-option key="27" label="浙江省" value="浙江省"></el-option>
                    </el-select>

                    <el-autocomplete
                            class="inline-input"
                            v-model="query.name"
                            :fetch-suggestions="querySearch"
                            placeholder="搜索用户名"
                            :trigger-on-focus="false"
                            @select="handleSelect"
                    >
                    </el-autocomplete>
                    <el-button type="primary"  icon="el-icon-search" @click="handleSearch"></el-button>
                </el-col>
            </div>

            <el-table :data="tableData" border class="table" ref="multipleTable"
                      header-cell-class-name="table-header"  @selection-change="handleSelectionChange" >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column>
                <el-table-column prop="username" width="80" label="用户名"></el-table-column>
                <el-table-column label="消费金额" width="80">
                    <template slot-scope="scope">￥{{scope.row.money}}</template>
                </el-table-column>
                <el-table-column prop="name" width="100" label="收货人姓名">
                    <template slot-scope="scope">{{scope.row.name}}</template>
                </el-table-column>
                <el-table-column prop="address" label="地址"></el-table-column>
                <el-table-column prop="phonenumber" label="手机号"></el-table-column>
                <el-table-column label="订单状态" align="center" >
                    <template slot-scope="scope">
                        <el-tag :type="scope.row.state==='已发货'?'success':(scope.row.state==='未发货'?'danger':'')">{{scope.row.state}}</el-tag>
                    </template>
                </el-table-column>

                <el-table-column prop="date" label="下单时间"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button type="text" icon="el-icon-lx-forward"  class="green" @click="toSend(scope.$index, scope.row)" >去发货</el-button>
                        <el-button type="text" icon="el-icon-news" class="black" @click="checkOrder(scope.row)">查看订单</el-button>
                        <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.$index, scope.row)" >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <div class="pagination">
                <el-pagination background layout="total, prev, pager, next"
                               :current-page="query.pageIndex" :page-size="query.pageSize" :total="pageTotal" @current-change="handlePageChange" ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="姓名">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="手机号">
                    <el-input v-model="form.phonenumber"></el-input>
                </el-form-item>
                <el-form-item label="地址">
                    <el-input v-model="form.address"></el-input>
                </el-form-item>
            </el-form>

            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>

        <el-dialog title="订单信息" :visible.sync="dialogTableVisible">
            <el-table :data="order">
                <el-table-column property="catagory" label="类别" width="150"></el-table-column>
                <el-table-column property="product" label="产品" width="200"></el-table-column>
                <el-table-column property="number" label="数量"></el-table-column>
            </el-table>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogTableVisible = false">确 定</el-button>
            </span>
        </el-dialog>


    </div>
</template>



<script>
    export default {
        name: 'basetable',
        data() {
            return {
                query: {
                    address: '',
                    name: '',
                    phonenumber:"",
                    pageIndex: 1,
                    pageSize: 10
                },

                tableData: [],
                restaurants: [],
                order:[],
                multipleSelection: [],
                delList: [],
                editVisible: false,
                dialogTableVisible:false,
                pageTotal: 0,
                form: {},
                idx: -1,
                id: -1
            };
        },

        created() {
            this.getData();
        },
        methods: {

            querySearch(queryString, cb) {
                var restaurants = this.restaurants;
                var results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants;
                // 调用 callback 返回建议列表的数据
                cb(results);
            },
            createFilter(queryString) {
                return (restaurant) => {
                    return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
                };
            },
            loadAll() {
                return this.restaurants;
            },

            handleSelect(item) {
                console.log(item);
            },


            getData() {
                this.$axios
                    .post('/table', {
                        params: ".ordertable."
                    })
                    .then(successResponse => {
                        const str = successResponse.data;
                        let arr = new Array();
                        arr=str.split(',');
                        for(let i=0; i<arr.length-1; i++)
                        {
                            let todoObj={
                                id: arr[i],
                                username: arr[++i],
                                money: arr[++i],
                                name:arr[++i],
                                address: arr[++i],
                                phonenumber:arr[++i],
                                state: arr[++i],
                                date: arr[++i],

                            };

                            this.tableData .push(todoObj);
                        }

                        for(let i=0; i<arr.length-1; i=i+8)
                        {
                            this.restaurants.push({
                                value:arr[i+1],
                                address:arr[i+4]
                            });
                        }
                        this.pageTotal = arr[arr.length-1] || 50;

                    })
            },

            // 触发搜索按钮
            handleSearch() {
                if(this.query.name=="" && this.query.address=="" ) {
                    this.$message.warning("输入为空");
                }
                if(this.query.name=="" && this.query.address!="" )
                {
                    this.$axios
                        .post('/table', {
                            params: ".orderSearch_address."+this.query.address+"."
                        })
                        .then(successResponse => {
                            const str = successResponse.data;
                            this.searchResult(str);
                        })
                }
                if(this.query.name!="" && this.query.address=="" )
                {
                    this.$axios
                        .post('/table', {
                            params: ".orderSearch_name."+this.query.name+"."
                        })
                        .then(successResponse => {
                            const str = successResponse.data;
                            this.searchResult(str);
                        })
                }
                if(this.query.name!="" && this.query.address!="" )
                {
                    this.$axios
                        .post('/table', {
                            params: ".orderSearch."+this.query.name+"."+this.query.address+"."
                        })
                        .then(successResponse => {
                            const str = successResponse.data;
                            this.searchResult(str);
                        })
                }
            },
            searchResult(str){
                this.tableData=[];
                let arr = new Array();
                arr=str.split(',');


                for(let i=0; i<arr.length-1; i++)
                {
                    let todoObj={
                        id: arr[i],
                        username: arr[++i],
                        money: arr[++i],
                        name:arr[++i],
                        address: arr[++i],
                        phonenumber:arr[++i],
                        state: arr[++i],
                        date: arr[++i],

                    };

                    this.tableData .push(todoObj);
                }
                this.pageTotal = arr[arr.length-1] || 50;
            },

            // 删除操作
            handleDelete(index, row) {
                // 二次确认删除
                this.$confirm('确定要删除吗？', '提示', {
                    type: 'warning'
                })

                    .then(() => {
                        this.$axios
                            .post('/table', {
                                params: ".del."+row.username+"."+row.date+"."
                            })
                            .then(successResponse => {
                                this.$message.success('删除成功');
                                this.tableData.splice(index, 1);
                            })

                    })
                    .catch(() => {});

            },

            // 多选操作
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },

            //批量删除
            delAllSelection() {
                if(this.delList.concat(this.multipleSelection)==""){
                    this.$message.warning("未选择删除项");
                }
                else{
                    let str = '';
                    const length = this.multipleSelection.length;
                    for(let i = 0; i < length; i++){
                        str=str+this.multipleSelection[i].username+"."+this.multipleSelection[i].date+".";
                        console.log("aa"+str);
                    }
                    this.$axios
                        .post('/table', {
                            params: ".delSomeOrder."+str
                        })
                        .then(successResponse => {
                            if(successResponse.data=="delSuccess"){
                                // console.log("yes");
                                const length = this.multipleSelection.length;
                                let str = '';
                                this.delList = this.delList.concat(this.multipleSelection);
                                for (let i = 0; i < length; i++) {
                                    str += this.multipleSelection[i].name + ' ';
                                    this.tableData.splice(this.multipleSelection[i].index,1);
                                }
                                this.$message.error(`删除了${str}`);
                                this.multipleSelection = [];
                            }
                        });
                }


            },

            // 编辑操作
            handleEdit(index, row) {
                this.idx = index;
                this.form = row;
                this.editVisible = true;
            },
            checkOrder(row){
                console.log("aa"+row.username+row.date);
                this.dialogTableVisible = true;
                this.$axios
                    .post('/table', {
                        params: ".checkOrder."+row.username+"."+row.date+"."
                    })
                    .then(successResponse => {
                        const str = successResponse.data;
                        let arr = new Array();
                        arr=str.split(',');

                        for(let i=0; i<arr.length-1; i++)
                        {
                            this.order.push({
                                catagory:arr[i],
                                product:arr[++i],
                                number:arr[++i]
                            });
                        }
                        // this.dialogTableVisible=false;
                        // this.$alert('<table style="border: 1px solid black;border-collapse:collapse;width:100%"> ' +
                        //     '<tr style=" border: 1px;"><td style="border: 1px solid #b4bccc;border-collapse:collapse;">列1</td><td style="border: 1px solid #b4bccc;border-collapse:collapse;">列2</td></tr>' +
                        //     '<tr style=" border: 1px;"><td style="border: 1px solid #b4bccc;border-collapse:collapse;">pr1</td><td style="border: 1px solid #b4bccc;border-collapse:collapse;">'+arr[0]+'</td></tr>'+
                        //     '<tr style=" border: 1px;"><td style="border: 1px solid #b4bccc;border-collapse:collapse;">pr2</td><td style="border: 1px solid #b4bccc;border-collapse:collapse;">'+arr[1]+'</td></tr>'+
                        //     '<tr style=" border: 1px;"><td style="border: 1px solid #b4bccc;border-collapse:collapse;">pr3</td><td style="border: 1px solid #b4bccc;border-collapse:collapse;">'+arr[2]+'</td></tr>', '订单详情', {
                        //     dangerouslyUseHTMLString: true,
                        //     center: true
                        // });
                    })
            },
            toSend(index,row){
                if(row.state=="已发货"){
                    this.$message.warning("已经发货了，不可重复发货");
                }
                else{
                    this.$axios
                        .post('/table', {
                            params: ".toSend."+row.name+"."
                        })
                        .then(successResponse => {
                            if (successResponse.data == "SendSuccess") {
                                this.$message.success("发货成功！");
                                row.state="已发货";
                                this.$set(this.tableData, this.index, row.state);

                            }
                        })
                }

                    },
            orderConfirm(){
                this.order_editVisible = false;
            },
            // 保存编辑
            saveEdit() {
                // console.log("aaa"+this.form.address);
                if(this.form.name==""||this.form.address==""||this.form.phonenumber=="")
                {
                    this.$message.warning("不可为空");
                }
                else{
                    this.$axios
                        .post('/table', {
                            params: ".editSendInfo."+this.form.name+"."+this.form.address+"."+this.form.phonenumber+"."
                        })
                        .then(successResponse => {
                            console.log(successResponse);
                            if(successResponse.data=="editSendInfoOk"){
                                this.editVisible = false;
                                this.$message.success(`修改第 ${this.idx + 1} 行成功`);
                                this.$set(this.tableData, this.idx, this.form);
                            }

                        })
                }

            },

            // 分页导航

            handlePageChange(val) {
                this.$set(this.query, 'pageIndex', val);
                this.getData();
            }

        },
        mounted() {
            this.restaurants = this.loadAll();
        },

    };

</script>



<style scoped>

    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }

    .table {
        width: 100%;
        font-size: 14px;
    }

    .red {
        color: #ff0000;
    }
    .green {
        color: #57cc4f;
    }
    .black {
        color: #5f77a3;
    }

    .mr10 {
        margin-right: 10px;
    }

</style>
<style>
    .my-autocomplete {
    li {
        line-height: normal;
        padding: 7px;



    .highlighted .addr {
        color: #ddd;
    }
    }
    }
</style>